<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yang-da-xiong.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="前言简单记录opencv有关图像操作部分的笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="opencv学习笔记之图像操作">
<meta property="og:url" content="https://yang-da-xiong.gitee.io/2022/02/09/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E5%83%8F%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="杨大熊的代码世界">
<meta property="og:description" content="前言简单记录opencv有关图像操作部分的笔记">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c02cf03050654c76ab732ddea2c28753.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/edb03eb11b4c40de8e42af4580fe1831.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/3d88ae6987624f6298a9df54e80a4d25.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/2b23e44ec2de4a70ba410ad786a363e2.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">
<meta property="article:published_time" content="2022-02-09T13:45:38.381Z">
<meta property="article:modified_time" content="2022-02-09T13:50:14.490Z">
<meta property="article:author" content="杨大熊">
<meta property="article:tag" content="c++">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/c02cf03050654c76ab732ddea2c28753.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70">

<link rel="canonical" href="https://yang-da-xiong.gitee.io/2022/02/09/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E5%83%8F%E6%93%8D%E4%BD%9C/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>opencv学习笔记之图像操作 | 杨大熊的代码世界</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">杨大熊的代码世界</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://yang-da-xiong.gitee.io/2022/02/09/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E5%83%8F%E6%93%8D%E4%BD%9C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="杨大熊">
      <meta itemprop="description" content="各类IT知识分享">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="杨大熊的代码世界">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          opencv学习笔记之图像操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-02-09 21:45:38 / 修改时间：21:50:14" itemprop="dateCreated datePublished" datetime="2022-02-09T21:45:38+08:00">2022-02-09</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>简单记录opencv有关图像操作部分的笔记</p>
<span id="more"></span>

<h1 id="一、辅助工具-Image-Watch"><a href="#一、辅助工具-Image-Watch" class="headerlink" title="一、辅助工具 Image Watch"></a>一、辅助工具 Image Watch</h1><p>Image Watch是一个免费且非常强大的图像数据查看插件，在进行图片查看时，可以显示其像素值和通道数，对于图片的操作后查看结果非常有帮助。</p>
<p>附下载地址</p>
<p>VS2019版：<a target="_blank" rel="noopener" href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch2019">Image Watch 2019 - Visual Studio Marketplace</a> </p>
<p>VS2017版：<a target="_blank" rel="noopener" href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch2017">Image Watch 2017 - Visual Studio Marketplace</a> </p>
<p>VS2015及以下版：<a target="_blank" rel="noopener" href="https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch">Image Watch - Visual Studio Marketplace</a> </p>
<p>打开方式在【视图】→【其他窗口】中找到image watch后打开，可以配合调试中的断点使用。</p>
<h1 id="二、Mat类"><a href="#二、Mat类" class="headerlink" title="二、Mat类"></a>二、Mat类</h1><p>创建方式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Mat a; //创建一个名为a的矩阵头</span><br><span class="line">a=imread(&quot;lena.jpg&quot;);</span><br></pre></td></tr></table></figure>

<h4 id="Opencv中的数据类型："><a href="#Opencv中的数据类型：" class="headerlink" title="Opencv中的数据类型："></a>Opencv中的数据类型：</h4><table>
<thead>
<tr>
<th>数据类型</th>
<th>具体类型</th>
<th>取值范围</th>
</tr>
</thead>
<tbody><tr>
<td>CV_8U</td>
<td>8位无符号整数</td>
<td>0~255</td>
</tr>
<tr>
<td>CV_8S</td>
<td>8位符号整数</td>
<td>-128~127</td>
</tr>
<tr>
<td>CV_16U</td>
<td>16位无符号整数</td>
<td>0~65535</td>
</tr>
<tr>
<td>CV_16S</td>
<td>16位符号整数</td>
<td>-32768~32767</td>
</tr>
<tr>
<td>CV_32S</td>
<td>32位符号整数</td>
<td>-2147483648~2147483647</td>
</tr>
<tr>
<td>CV_32F</td>
<td>32位浮点整数</td>
<td></td>
</tr>
<tr>
<td>CV_64F</td>
<td>64位浮点整数</td>
<td></td>
</tr>
</tbody></table>
<p>关于通道，C1、C2、C3、C4分别表示单通道、双通道、3通道以及4通道。</p>
<h4 id="Mat类的几种构造方式："><a href="#Mat类的几种构造方式：" class="headerlink" title="Mat类的几种构造方式："></a>Mat类的几种构造方式：</h4><p>方式1：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Mat(int rows,int cols,int type)</span><br></pre></td></tr></table></figure>

<p>例如 Mat a（640,480，CV_8UC3）表示创建了一个640*480的8位无符号整数的3通道矩阵用于存放彩色图像</p>
<p>方式2：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Mat(Size size(),int type)</span><br></pre></td></tr></table></figure>

<p>例如 Mat b（Size（480,640），CV_32FC3）表示创建了一个640*480的32位浮点整数的3通道矩阵用于存放彩色图像</p>
<h4 id="Mat类赋值："><a href="#Mat类赋值：" class="headerlink" title="Mat类赋值："></a>Mat类赋值：</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Mat(int rows,int cols,int type,const Scalar &amp;s)</span><br></pre></td></tr></table></figure>

<p>例如：Mat img1(2, 2, CV_8UC3, Scalar(0, 0, 255))</p>
<p>创建了一个三通道的矩阵，第三层通道值为255，前两层为0，启动Image Watch查看效果如下：</p>
<img src="https://img-blog.csdnimg.cn/c02cf03050654c76ab732ddea2c28753.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="60%">

<p>其他方式（枚举法）：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Mat a=(Mat_&lt;int&gt;(3,3)&lt;&lt;1,2,3,4,5,6,7,8,9)</span><br></pre></td></tr></table></figure>

<img src="https://img-blog.csdnimg.cn/edb03eb11b4c40de8e42af4580fe1831.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="60%">

<h4 id="Mat类还可支持加减乘除等操作"><a href="#Mat类还可支持加减乘除等操作" class="headerlink" title="Mat类还可支持加减乘除等操作"></a>Mat类还可支持加减乘除等操作</h4><h4 id="Mat类元素的读取"><a href="#Mat类元素的读取" class="headerlink" title="Mat类元素的读取"></a>Mat类元素的读取</h4><p>常用方式：</p>
<p>通过at方法读取<strong>单通道</strong>Mat类矩阵中的元素</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Mat a=(Mat_&lt;uchar&gt;(3,3)&lt;&lt;1,2,3,4,5,6,7,8,9);</span><br><span class="line">int value=(int)a.at&lt;uchar&gt;(0,0);</span><br></pre></td></tr></table></figure>

<p>对于多通道类型，存储方式见下图</p>
<p>3通道的存储方式：</p>
<img src="https://img-blog.csdnimg.cn/3d88ae6987624f6298a9df54e80a4d25.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="60%">

<p>图示为一个3*3通道的存储方式，对于单个元素，是按照每个通道相邻这样去存储的。</p>
<p>那么需要特殊的数据结构去存放这些Vec3b，Vec3s，Vec3w，Vec3d，Vec3f，Vec3i</p>
<p>通过at方法读取<strong>多通道</strong>Mat类矩阵中的元素</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Mat b(3,4,CV_8UC3,Scalar(0,0,1));</span><br><span class="line">Vec3b vc3 = b.at&lt;Vec3b&gt;(0, 0);</span><br><span class="line">int first = (int)vc3.val[0];</span><br><span class="line">int second = (int)vc3.val[1];</span><br><span class="line">int third = (int)vc3.val[2];</span><br></pre></td></tr></table></figure>

<p>强制转换不能少！</p>
<p>其他还有指针ptr和迭代器方式，这里不做记录了</p>
<h1 id="二、图像操作"><a href="#二、图像操作" class="headerlink" title="二、图像操作"></a>二、图像操作</h1><p>图像操作是opencv的重头戏，从几个函数分别介绍</p>
<h4 id="1-imread-函数—-图像读取"><a href="#1-imread-函数—-图像读取" class="headerlink" title="1.imread()函数—-图像读取"></a>1.imread()函数—-图像读取</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">imread(const String&amp;filename,int flags=IMREAD_COLOR)</span><br></pre></td></tr></table></figure>

<p>其中第一个参数为图像的完整地址，如“D:/vs项目/opencv4_test/opencv4_test/lena.jpg”，第二个参数为读取方式，比如以灰度图读取（IMREAD_GRAYSCALE）、彩色图读取（IMREAD_COLOR）</p>
<h4 id="2-imshow-函数—-图像显示"><a href="#2-imshow-函数—-图像显示" class="headerlink" title="2.imshow()函数—-图像显示"></a>2.imshow()函数—-图像显示</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">imshow(const String &amp; winname,InputArray mat)</span><br></pre></td></tr></table></figure>

<p>其中第一个参数为要显示图像的窗口名字，第二个参数为要显示的图像矩阵</p>
<h4 id="3-namedWindow-函数—-图像显示窗口"><a href="#3-namedWindow-函数—-图像显示窗口" class="headerlink" title="3.namedWindow()函数—-图像显示窗口"></a>3.namedWindow()函数—-图像显示窗口</h4><p>该函数为图像窗口函数，用于创建图像要显示的窗口</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void namedWindow(const String &amp; winname,int flags=WINDOW_AUTOSIZE)</span><br></pre></td></tr></table></figure>

<h4 id="4-imwrite-函数—-图像保存"><a href="#4-imwrite-函数—-图像保存" class="headerlink" title="4.imwrite()函数—-图像保存"></a>4.imwrite()函数—-图像保存</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bool imwrite(const String &amp; filename,InputArray img,Const vector&lt;int&gt;&amp; params =vector&lt;int&gt;())</span><br></pre></td></tr></table></figure>

<p>filename：保存图像的地址和文件名</p>
<p>img：将要保存的Mat类矩阵变量</p>
<p>params：保存图片格式属性设置标志</p>
<h5 id="imwrite-函数使用示例程序："><a href="#imwrite-函数使用示例程序：" class="headerlink" title="imwrite()函数使用示例程序："></a>imwrite()函数使用示例程序：</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;opencv2\opencv.hpp&gt;</span>  <span class="comment">//加载OpenCV4的头文件</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> cv; <span class="comment">//OpenCV命名空间</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">AlphaMat</span><span class="params">(Mat &amp;mat)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	CV_Assert(mat.channels() == <span class="number">4</span>);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; mat.rows; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;mat.cols;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			Vec4b&amp; bgra = mat.at&lt;Vec4b&gt;(i, j);</span><br><span class="line">			bgra[<span class="number">0</span>] = UCHAR_MAX;  <span class="comment">//蓝色通道B</span></span><br><span class="line">			bgra[<span class="number">1</span>] = saturate_cast&lt;uchar&gt;((<span class="keyword">float</span>(mat.cols - j)) / ((<span class="keyword">float</span>)mat.cols)*UCHAR_MAX); <span class="comment">//绿色通道G</span></span><br><span class="line">			bgra[<span class="number">2</span>] = saturate_cast&lt;uchar&gt;((<span class="keyword">float</span>(mat.cols - i)) / ((<span class="keyword">float</span>)mat.rows)*UCHAR_MAX); <span class="comment">//红色通道R</span></span><br><span class="line">			bgra[<span class="number">3</span>] = saturate_cast&lt;uchar&gt;(<span class="number">0.5</span>*(bgra[<span class="number">1</span>] + bgra[<span class="number">2</span>])); <span class="comment">//Alpha通道</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="function">Mat <span class="title">mat</span><span class="params">(<span class="number">480</span>, <span class="number">640</span>, CV_8UC4)</span></span>;</span><br><span class="line">	AlphaMat(mat);</span><br><span class="line">	<span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; compression_params;</span><br><span class="line">	compression_params.push_back(IMWRITE_PNG_COMPRESSION);  <span class="comment">//PNG格式图像压缩标志</span></span><br><span class="line">	compression_params.push_back(<span class="number">9</span>);  <span class="comment">//设置最高压缩质量</span></span><br><span class="line">	<span class="keyword">bool</span> result = imwrite(<span class="string">&quot;alpha.png&quot;</span>, mat, compression_params);</span><br><span class="line">	<span class="keyword">if</span> (!result)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;保存成PNG格式图像失败&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;保存成功&quot;</span> &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;  <span class="comment">//程序结束</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src="https://img-blog.csdnimg.cn/2b23e44ec2de4a70ba410ad786a363e2.png?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NpbmF0XzIwMTc3MzI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" width="70%">

<p>转换前与转换后成功保存的图像对比，4通道（Alpha通道）图像可以保存成PNG格式文件。</p>
<p>感谢阅读！也欢迎大家关注小白博主，多多鼓励一下！ </p>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>杨大熊
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yang-da-xiong.gitee.io/2022/02/09/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E5%83%8F%E6%93%8D%E4%BD%9C/" title="opencv学习笔记之图像操作">https://yang-da-xiong.gitee.io/2022/02/09/opencv学习笔记之图像操作/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/c/" rel="tag"># c++</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/02/08/%E7%AE%97%E6%B3%95%E7%BB%83%E4%B9%A0%E9%A2%9827---%E8%93%9D%E6%A1%A5%E6%9D%AF2020%E7%9C%81%E8%B5%9B%E2%80%9C%E8%A3%85%E9%A5%B0%E7%8F%A0%E2%80%9D/" rel="prev" title="算法练习题27---蓝桥杯2020省赛“装饰珠”">
      <i class="fa fa-chevron-left"></i> 算法练习题27---蓝桥杯2020省赛“装饰珠”
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/02/10/opencv%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E4%B9%8B%E5%9B%BE%E5%83%8F%E6%A8%A1%E5%9E%8B/" rel="next" title="opencv学习笔记之图像模型">
      opencv学习笔记之图像模型 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BE%85%E5%8A%A9%E5%B7%A5%E5%85%B7-Image-Watch"><span class="nav-number">2.</span> <span class="nav-text">一、辅助工具 Image Watch</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81Mat%E7%B1%BB"><span class="nav-number">3.</span> <span class="nav-text">二、Mat类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Opencv%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%EF%BC%9A"><span class="nav-number">3.0.0.1.</span> <span class="nav-text">Opencv中的数据类型：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Mat%E7%B1%BB%E7%9A%84%E5%87%A0%E7%A7%8D%E6%9E%84%E9%80%A0%E6%96%B9%E5%BC%8F%EF%BC%9A"><span class="nav-number">3.0.0.2.</span> <span class="nav-text">Mat类的几种构造方式：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Mat%E7%B1%BB%E8%B5%8B%E5%80%BC%EF%BC%9A"><span class="nav-number">3.0.0.3.</span> <span class="nav-text">Mat类赋值：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Mat%E7%B1%BB%E8%BF%98%E5%8F%AF%E6%94%AF%E6%8C%81%E5%8A%A0%E5%87%8F%E4%B9%98%E9%99%A4%E7%AD%89%E6%93%8D%E4%BD%9C"><span class="nav-number">3.0.0.4.</span> <span class="nav-text">Mat类还可支持加减乘除等操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Mat%E7%B1%BB%E5%85%83%E7%B4%A0%E7%9A%84%E8%AF%BB%E5%8F%96"><span class="nav-number">3.0.0.5.</span> <span class="nav-text">Mat类元素的读取</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%9B%BE%E5%83%8F%E6%93%8D%E4%BD%9C"><span class="nav-number">4.</span> <span class="nav-text">二、图像操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-imread-%E5%87%BD%E6%95%B0%E2%80%94-%E5%9B%BE%E5%83%8F%E8%AF%BB%E5%8F%96"><span class="nav-number">4.0.0.1.</span> <span class="nav-text">1.imread()函数—-图像读取</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-imshow-%E5%87%BD%E6%95%B0%E2%80%94-%E5%9B%BE%E5%83%8F%E6%98%BE%E7%A4%BA"><span class="nav-number">4.0.0.2.</span> <span class="nav-text">2.imshow()函数—-图像显示</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-namedWindow-%E5%87%BD%E6%95%B0%E2%80%94-%E5%9B%BE%E5%83%8F%E6%98%BE%E7%A4%BA%E7%AA%97%E5%8F%A3"><span class="nav-number">4.0.0.3.</span> <span class="nav-text">3.namedWindow()函数—-图像显示窗口</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-imwrite-%E5%87%BD%E6%95%B0%E2%80%94-%E5%9B%BE%E5%83%8F%E4%BF%9D%E5%AD%98"><span class="nav-number">4.0.0.4.</span> <span class="nav-text">4.imwrite()函数—-图像保存</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#imwrite-%E5%87%BD%E6%95%B0%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B%E7%A8%8B%E5%BA%8F%EF%BC%9A"><span class="nav-number">4.0.0.4.1.</span> <span class="nav-text">imwrite()函数使用示例程序：</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="杨大熊"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">杨大熊</p>
  <div class="site-description" itemprop="description">各类IT知识分享</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">58</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Yang-Da-xiong?tab=repositories" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Yang-Da-xiong?tab&#x3D;repositories" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45775703?spm=1000.2115.3001.5343" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45775703?spm&#x3D;1000.2115.3001.5343" rel="noopener" target="_blank"><i class="fab fa-codiepie fa-fw"></i>CSDN</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">杨大熊</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>

<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>

<div class="theme-info">
<div class="powered-by"></div>
<span class="post-count">博客全站共56.5k字</span>
</div>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"log":false});</script></body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/schemes/clicklove.js"></script>
